package org.example.managere.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 添加资源处理器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 明确指定静态资源路径，排除API路径
        registry.addResourceHandler("/css/**", "/js/**", "/images/**", "/*.html", "/*.ico", "/")
                .addResourceLocations("classpath:/static/css/", "classpath:/static/js/", 
                        "classpath:/static/images/", "classpath:/static/");
    }

    /**
     * 添加视图控制器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 将根路径重定向到index.html
        registry.addViewController("/").setViewName("forward:/index.html");
        // 处理SPA刷新问题
        registry.addViewController("/index").setViewName("forward:/index.html");
    }
    
    /**
     * 配置路径匹配，确保API请求正确匹配到控制器方法
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // API路径前缀匹配规则配置
        configurer.setUseTrailingSlashMatch(false); // 不使用尾部斜杠匹配
    }
} 